package com.bysj.studentsystem.controller;

import com.bysj.studentsystem.entity.SysClass;
import com.bysj.studentsystem.service.SysClassService;
import com.bysj.studentsystem.vo.ResponseDataVO;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (SysClass)表控制层
 *
 * @author makejava
 * @since 2024-03-12 20:55:09
 */
@RestController
@RequestMapping("/class")
public class SysClassController {
    /**
     * 服务对象
     */
    @Resource
    private SysClassService sysClassService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping
    public ResponseDataVO<PageInfo<SysClass>> queryByPage(@RequestParam(value = "name", required = false) String name,
                                                          @RequestParam(value = "classTeacherId", required = false) Integer classTeacherId,
                                                          @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                          @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            pageNum = 1;
            pageSize = 10;
        }
        SysClass sysClass = new SysClass();
        sysClass.setName(name);
        sysClass.setClassTeacherId(classTeacherId);
        return new ResponseDataVO().ok(this.sysClassService.queryByPage(sysClass, pageNum, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseDataVO<SysClass> queryById(@PathVariable("id") Integer id) {
        return new ResponseDataVO().ok(this.sysClassService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysClass 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseDataVO<SysClass> add(@RequestBody SysClass sysClass) {
        return new ResponseDataVO().ok(this.sysClassService.insert(sysClass));
    }

    /**
     * 编辑数据
     *
     * @param sysClass 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseDataVO<SysClass> edit(@RequestBody SysClass sysClass) {
        return new ResponseDataVO().ok(this.sysClassService.update(sysClass));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseDataVO<Boolean> deleteById(@RequestParam("id") Integer id) {
        return new ResponseDataVO().ok(this.sysClassService.deleteById(id));
    }

}

